AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )

function ENT:Initialize()
	self.Entity:SetModel( "models/props_c17/substation_transformer01b.mdl" )
	--"models/props_citizen_tech/SteamEngine001a.mdl"
	--models/props_c17/substation_transformer01b.mdl
	self.Entity:PhysicsInit( SOLID_VPHYSICS ) 	
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS ) 
	self.Entity:SetUseType(SIMPLE_USE)  
	RD.RegisterNonStorageDevice(self)	
	
	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
		phys:EnableGravity(true)
		phys:EnableDrag(true)
		phys:EnableCollisions(true)
		phys:EnableMotion(true)
	end
	
	--RD.AddResource(self, "energy", 0)
	--RD.AddResource(self, "coolant", 0)
	--RD.AddResource(self, "water", 0)
	--RD.AddResource(self, "heavy water", 0)	
	--RD.AddResource(self, "Blue Ice", 0)
	--RD.AddResource(self, "Clear Ice", 0)
	--RD.AddResource(self, "Glacial Mass", 0)
	--RD.AddResource(self, "White Glaze", 0)
	--RD.AddResource(self, "Dark Glitter", 0)
	--RD.AddResource(self, "Glare Crust", 0)
	--RD.AddResource(self, "Gelidus", 0)
	--RD.AddResource(self, "Krystallos", 0)
	
	--RD.AddResource(self, "Oxygen Isotopes", 0)
	--RD.AddResource(self, "Hydrogen Isotopes", 0)
	--RD.AddResource(self, "Helium Isotopes", 0)
	--RD.AddResource(self, "Nitrogen Isotopes", 0)
	--RD.AddResource(self, "Liquid Ozone", 0)
	--RD.AddResource(self, "Strontium Clathrates", 0)
	
	self.Inputs = Wire_CreateInputs( self.Entity, { "Activate" } ) 
	self.Outputs = Wire_CreateOutputs( self.Entity, { "Active", "Cycle %" } )
	
	self.RI1 = false
	self.RI2 = false
	self.RI3 = false
	self.RI4 = false
	self.RI5 = false
	self.RI6 = false
	self.RI7 = false
	self.RI8 = false
	
	self.CanRefine = false
	self.CycleEnd = CurTime()
	self.NextCycle = CurTime()
	self.Cycles = 0
	
				
end

function ENT:SpawnFunction( ply, tr )
if ( !tr.Hit ) then return end    	
	local SpawnPos = tr.HitPos + tr.HitNormal * 100 	
	local ent = ents.Create( "ice_refinery_ship" )
		ent:SetPos( SpawnPos )
		ent:Spawn()
		ent:Activate() 
	return ent
end

function ENT:OnRemove()
	
end

function ENT:StartRefine()

	local energy = RD.GetResourceAmount(self, "energy")
	local energyneed = self.CycleEnergy/self.CycleTime
	
	local ice1 = RD.GetResourceAmount(self, "Blue Ice") 
	local ice2 = RD.GetResourceAmount(self, "Clear Ice") 
	local ice3 = RD.GetResourceAmount(self, "Glacial Mass") 
	local ice4 = RD.GetResourceAmount(self, "White Glaze") 
	local ice5 = RD.GetResourceAmount(self, "Dark Glitter") 
	local ice6 = RD.GetResourceAmount(self, "Glare Crust") 
	local ice7 = RD.GetResourceAmount(self, "Gelidus") 
	local ice8 = RD.GetResourceAmount(self, "Krystallos")
	
	if energy >= energyneed then
	
		if ice1 >= self.CycleVol then
			RD.ConsumeResource(self, "Blue Ice", self.CycleVol)
			self.RI1 = true
			self.CanRefine = true
			Msg("Got Ice1 \n")
		end 
		if ice2 >= self.CycleVol then
			RD.ConsumeResource(self, "Clear Ice", self.CycleVol)
			self.RI2 = true
			self.CanRefine = true
			Msg("Got Ice2 \n")
		end
		if ice3 >= self.CycleVol then
			RD.ConsumeResource(self, "Glacial Mass", self.CycleVol)
			self.RI3 = true
			self.CanRefine = true
			Msg("Got Ice3 \n")
		end
		if ice4 >= self.CycleVol then
			RD.ConsumeResource(self, "White Glaze", self.CycleVol)
			self.RI4 = true
			self.CanRefine = true
			Msg("Got Ice4 \n")
		end
		if ice5 >= self.CycleVol then
			RD.ConsumeResource(self, "Dark Glitter", self.CycleVol)
			self.RI5 = true
			self.CanRefine = true
			Msg("Got Ice5 \n")
		end
		if ice6 >= self.CycleVol then
			RD.ConsumeResource(self, "Glare Crust", self.CycleVol)
			self.RI6 = true
			self.CanRefine = true
			Msg("Got Ice6 \n")
		end
		if ice7 >= self.CycleVol then
			RD.ConsumeResource(self, "Gelidus", self.CycleVol)
			self.RI7 = true
			self.CanRefine = true
			Msg("Got Ice7 \n")
		end
		if ice8 >= self.CycleVol then
			RD.ConsumeResource(self, "Krystallos", self.CycleVol)
			self.RI8 = true
			self.CanRefine = true
			Msg("Got Ice8 \n")
		end
		
		if self.CanRefine then
			RD.ConsumeResource(self, "energy", energyneed)
			self.NextCycle = CurTime() + 1
			Msg("Can Refine From Start Refine \n")			
		end	
			
	end     	
	
end

function ENT:Refine()
	
	if self.Cycles >= self.CycleTime then
		self:EndRefine()
		Msg("Refining done, relaying from Refine to EndRefine \n")		
		return false
	end
	
	local energy = RD.GetResourceAmount(self, "energy")
	local energyneed = self.CycleEnergy/self.CycleTime
	
	if energy >= energyneed then
		RD.ConsumeResource(self, "energy", energyneed)
		self.NextCycle = self.NextCycle + 1
		self.Cycles = self.Cycles + 1
		Msg("Cycles: "..tostring(self.Cycles).."\n")
	elseif energy < energyneed then 
		self.NextCycle = self.NextCycle + 1
		Msg("Ran out of energy mid cycle \n")		
	end  	
	 
end

function ENT:EndRefine()
	self.CanRefine = false
	
	if self.RI1 then
		local give = AM_GetRefined("Blue Ice", self.RefineEfficiency)
		--RD.SupplyResource(self, "coolant", give.coolant)
		RD.SupplyResource(self, "water", give.water)
		--RD.SupplyResource(self, "heavy water", give.heavywater)
		RD.SupplyResource(self, "Oxygen Isotopes", give.oxygen)
		RD.SupplyResource(self, "Hydrogen Isotopes", give.hydrogen)
		--RD.SupplyResource(self, "Helium Isotopes", give.helium)
		RD.SupplyResource(self, "Nitrogen Isotopes", give.nitrogen)
		--RD.SupplyResource(self, "Liquid Ozone", give.ozone)
		--RD.SupplyResource(self, "Strontium Clathrates", give.strontium)
		self.RI1 = false	
	end
	if self.RI2 then
		local give = AM_GetRefined("Clear Ice", self.RefineEfficiency)
		--RD.SupplyResource(self, "coolant", give.coolant)
		RD.SupplyResource(self, "water", give.water)
		--RD.SupplyResource(self, "heavy water", give.heavywater)
		RD.SupplyResource(self, "Oxygen Isotopes", give.oxygen)
		RD.SupplyResource(self, "Hydrogen Isotopes", give.hydrogen)
		--RD.SupplyResource(self, "Helium Isotopes", give.helium)
		RD.SupplyResource(self, "Nitrogen Isotopes", give.nitrogen)
		--RD.SupplyResource(self, "Liquid Ozone", give.ozone)
		--RD.SupplyResource(self, "Strontium Clathrates", give.strontium)
		self.RI2 = false	
	end
	if self.RI3 then
		local give = AM_GetRefined("Glacial Mass", self.RefineEfficiency)
		--RD.SupplyResource(self, "coolant", give.coolant)
		RD.SupplyResource(self, "water", give.water)
		--RD.SupplyResource(self, "heavy water", give.heavywater)
		RD.SupplyResource(self, "Oxygen Isotopes", give.oxygen)
		RD.SupplyResource(self, "Hydrogen Isotopes", give.hydrogen)
		--RD.SupplyResource(self, "Helium Isotopes", give.helium)
		RD.SupplyResource(self, "Nitrogen Isotopes", give.nitrogen)
		--RD.SupplyResource(self, "Liquid Ozone", give.ozone)
		--RD.SupplyResource(self, "Strontium Clathrates", give.strontium)
		self.RI3 = false	
	end
	if self.RI4 then
		local give = AM_GetRefined("White Glaze", self.RefineEfficiency)
		--RD.SupplyResource(self, "coolant", give.coolant)
		RD.SupplyResource(self, "water", give.water)
		--RD.SupplyResource(self, "heavy water", give.heavywater)
		RD.SupplyResource(self, "Oxygen Isotopes", give.oxygen)
		RD.SupplyResource(self, "Hydrogen Isotopes", give.hydrogen)
		--RD.SupplyResource(self, "Helium Isotopes", give.helium)
		RD.SupplyResource(self, "Nitrogen Isotopes", give.nitrogen)
		--RD.SupplyResource(self, "Liquid Ozone", give.ozone)
		--RD.SupplyResource(self, "Strontium Clathrates", give.strontium)
		self.RI4 = false	
	end
	if self.RI5 then
		local give = AM_GetRefined("Dark Glitter", self.RefineEfficiency)
		--RD.SupplyResource(self, "coolant", give.coolant)
		RD.SupplyResource(self, "water", give.water)
		--RD.SupplyResource(self, "heavy water", give.heavywater)
		RD.SupplyResource(self, "Oxygen Isotopes", give.oxygen)
		RD.SupplyResource(self, "Hydrogen Isotopes", give.hydrogen)
		--RD.SupplyResource(self, "Helium Isotopes", give.helium)
		RD.SupplyResource(self, "Nitrogen Isotopes", give.nitrogen)
		--RD.SupplyResource(self, "Liquid Ozone", give.ozone)
		--RD.SupplyResource(self, "Strontium Clathrates", give.strontium)
		self.RI5 = false	
	end
	if self.RI6 then
		local give = AM_GetRefined("Glare Crust", self.RefineEfficiency)
		--RD.SupplyResource(self, "coolant", give.coolant)
		RD.SupplyResource(self, "water", give.water)
		--RD.SupplyResource(self, "heavy water", give.heavywater)
		RD.SupplyResource(self, "Oxygen Isotopes", give.oxygen)
		RD.SupplyResource(self, "Hydrogen Isotopes", give.hydrogen)
		--RD.SupplyResource(self, "Helium Isotopes", give.helium)
		RD.SupplyResource(self, "Nitrogen Isotopes", give.nitrogen)
		--RD.SupplyResource(self, "Liquid Ozone", give.ozone)
		--RD.SupplyResource(self, "Strontium Clathrates", give.strontium)
		self.RI6 = false	
	end
	if self.RI7 then
		local give = AM_GetRefined("Gelidus", self.RefineEfficiency)
		--RD.SupplyResource(self, "coolant", give.coolant)
		RD.SupplyResource(self, "water", give.water)
		--RD.SupplyResource(self, "heavy water", give.heavywater)
		RD.SupplyResource(self, "Oxygen Isotopes", give.oxygen)
		RD.SupplyResource(self, "Hydrogen Isotopes", give.hydrogen)
		--RD.SupplyResource(self, "Helium Isotopes", give.helium)
		RD.SupplyResource(self, "Nitrogen Isotopes", give.nitrogen)
		--RD.SupplyResource(self, "Liquid Ozone", give.ozone)
		--RD.SupplyResource(self, "Strontium Clathrates", give.strontium)
		self.RI7 = false	
	end
	if self.RI8 then
		local give = AM_GetRefined("Krystallos", self.RefineEfficiency)
		--RD.SupplyResource(self, "coolant", give.coolant)
		RD.SupplyResource(self, "water", give.water)
		--RD.SupplyResource(self, "heavy water", give.heavywater)
		RD.SupplyResource(self, "Oxygen Isotopes", give.oxygen)
		RD.SupplyResource(self, "Hydrogen Isotopes", give.hydrogen)
		--RD.SupplyResource(self, "Helium Isotopes", give.helium)
		RD.SupplyResource(self, "Nitrogen Isotopes", give.nitrogen)
		--RD.SupplyResource(self, "Liquid Ozone", give.ozone)
		--RD.SupplyResource(self, "Strontium Clathrates", give.strontium)
		self.RI8 = false	
	end
	
	Msg("End Refine done. \n")
	
	self.NextCycle = self.NextCycle + 1
	self.Cycles = 0

end


function ENT:Think()
	self.BaseClass.Think(self)
	
	if self.CanRefine and CurTime() >= self.NextCycle then
		self:Refine()
	end
	if self.Cycles == 0 and self.InputActive and self.CanRefine == false then
		self:StartRefine()
	end
	
	self:Displays()
	 
end

function ENT:Displays()
	local percent = math.Round((self.Cycles/self.CycleTime)*100)
	local active = 0
	if self.CanRefine then
		active = 1
	else 
		active = 0
	end 
	
	self:SetOverlayText(self.PrintName.."\n".."Cycle: "..percent.."%")

	Wire_TriggerOutput(self.Entity,"Active",active)
	Wire_TriggerOutput(self.Entity,"Cycle %",percent)

end

function ENT:TriggerInput(iname, value)
	if (iname == "Activate") then
		if value == 1 then
			self.InputActive = true
			if self.Cycles == 0 and self.CanRefine == false then
				self:StartRefine()
			end		
		else
			self.InputActive = false			
		end
	end
end

function ENT:PreEntityCopy()
	RD.BuildDupeInfo(self.Entity)
	--build the DupeInfo table and save it as an entity mod
	local DupeInfo = self:BuildDupeInfo()
	if(DupeInfo) then
		duplicator.StoreEntityModifier(self.Entity,"WireDupeInfo",DupeInfo)
	end
end

function ENT:PostEntityPaste(Player,Ent,CreatedEntities)
	RD.ApplyDupeInfo(Ent, CreatedEntities)
	--apply the DupeInfo
	if(Ent.EntityMods and Ent.EntityMods.WireDupeInfo) then
		self.Owner = Player	
		Ent:ApplyDupeInfo(Player, Ent, Ent.EntityMods.WireDupeInfo, function(id) return CreatedEntities[id] end)
	end
end



